<?php
/**
 * Description: 获取每日一面用户答案列表
 * User: devforma
 * Date: 16/3/7
 * Time: 11:09
 */

class GetInterviewAnswers implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['question_id'])
            || !filter_var($queryArgs['question_id'], FILTER_VALIDATE_INT)) {
            return genOutput(1000);
        }

        //检测用户是否购买过面试
        if (!$this->checkPurchase($queryArgs['user_id'])) {
            return genOutput(1000);
        }

        //2016-06-23 app端上传文件有问题,取最早的30条用户答案
        $data = $cmsdb->getResults('SELECT `id`, `uid`, `duration`, `filename`, `hot` FROM `data_interview_users` WHERE `qid`=:qid AND `status`=1 ORDER BY `id` ASC LIMIT 30',
            array(':qid' => $queryArgs['question_id']));


        //获取用户昵称
        $userIds = array();
        foreach ($data as $d) {
            $userIds[] = $d->uid;
        }
        $userInfo = $this->getUserInfo($userIds);


        $list = array();
        foreach ($data as $d) {
            $list[] = array(
                'question_id' => intval($queryArgs['question_id']),
                'record_id' => intval($d->id),
                'user_id' => intval($d->uid),
                'duration' => intval($d->duration),
                'hot' => intval($d->hot),
                'nickname' => empty($userInfo[$d->uid]['nickname']) ? '' : $userInfo[$d->uid]['nickname'],
                'avatar' => empty($userInfo[$d->uid]['avatar']) ? '' : $userInfo[$d->uid]['avatar'],
                'file_url' => "http://edu-1.b0.upaiyun.com/interview/user_answer/{$d->uid}/{$queryArgs['question_id']}/{$d->filename}"
            );
        }

        return genOutput(1, array('list' => $list));
    }


    /**
     * 获取用户信息
     *
     * @param $userIds
     * @return array
     */
    private function getUserInfo($userIds) {
        global $uddb;

        $string = implode(',', $userIds);

        $data = $uddb->getResults("SELECT `user_id`, `nickname`, `avatar` FROM `user_social_info` WHERE `user_id` IN ({$string})");

        $list = array();
        foreach ($data as $d) {
            $list[$d->user_id] = array(
                'avatar' => empty($d->avatar) ? '' : self::avatar($d->avatar),
                'nickname' => empty($d->nickname) ? '匿名' : $d->nickname
            );
        }

        return $list;
    }


    /**
     * 头像url处理
     *
     * @param $avatar
     * @return string
     */
    static function avatar($avatar) {
        if (strpos($avatar, 'http://') === false) {
            return 'http://edu.appublisher.com/user-images/avatar/'.$avatar;
        }

        return $avatar;
    }


    /**
     * 检测用户是否开启每日一面
     *
     * @param $userId
     * @return null|string
     */
    private function checkPurchase($userId) {
        global $cmsdb;

        return $cmsdb->getVar('SELECT `id` FROM `relation_user_product` WHERE `user_id`=:uid AND `product_id`=1128 AND `product_type`="interview"',
            array(':uid' => $userId));
    }
}